#!/usr/bin/python3
# -*-coding:utf-8 -*-
# @Author  : caoyn
# @Time    : 2021/1/26 12:20 上午
# @Software: PyCharm

# 导入requests包
import requests
# 导入 random(随机数) 模块
import random
import time
import string
import pytest

"""
作业3、日常执行回归的用例，尝试转化为一个自动化用例；
"""


def rand_str(length):
    """
    
    生成指定长度的随机字符串
    string.digits=0123456789
    string.ascii_letters=abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    :param length:
    :return:
    """
    str_list = [random.choice(string.digits + string.ascii_letters) for _ in range(length)]
    return ''.join(str_list)


def get_token(ua, pw, dm, ev):
    """
    获取token
    :param ua: 18701572385
    :param pw: nan123456
    :param dm: u31tdnagdkg7/mxkx5ur9gc
    :param ev: online
    :return:
    """
    url = 'http://172.18.22.177:8000/login?username=%s&password=%s&domain=%s&env_name=%s' % (ua, pw, dm, ev)
    return requests.get(url)


# 定义一个随机字符串，多个字符中选取指定数量的字符组成新字符串
# number = ''.join(random.sample(
#     ['z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd',
#       'c', 'b', 'a'], 5))

# 新增商品品牌
def test_add_ProductBrand():
    headers = {
        "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI3MDAzZjZlMC1mZWYxLTQ5NTgtOWFjMy02OTg1NTdjMWRlMGUiLCJpYXQiOjE2MTQyMTY5ODZ9.eIMX07KSXB2yY8dkKAj5qGbyD3Vz5O3sApTSqAYfhi0",
        "content-type": "application/json; charset=utf-8",
        "Connection": "keep-alive",
    }

    # 发送一个新增商品品牌的请求
    url = "https://cloud.chanjet.com/hsy/u31tdnagdkg7/mxkx5ur9gc/entity/ProductBrand"
    # 新增一个"启用"状态的商品品牌，设置请求参数
    data1 = {"name": rand_str(5), "statusEnum": "A"}

    # 使用requests发送一个post请求
    response = requests.post(url=url, json=data1, headers=headers)
    result = response.json()
    assert response.status_code == 200
    ProductBrand_id = result["id"]
    ProductBrand_code = result["code"]
    ProductBrand_name = result["name"]

    # 打印响应结果
    print('响应结果：', response.text)
    print('品牌id：', ProductBrand_id)
    print('品牌编码 ：', ProductBrand_code)
    print('品牌名称：', ProductBrand_name)

    print("新增品牌成功")

    # 校验品牌名称重复
    data2 = {"name": ProductBrand_name, "statusEnum": "A"}
    response = requests.post(url=url, json=data2, headers=headers)
    result = response.json()
    print('响应结果：', response.text)
    assert response.status_code == 400
    assert result["code"] == "product.e1002"
    assert result["msg"] == "品牌名称重复"

    print("新增品牌失败，品牌名称重复")

    # 校验状态码无效，新增品牌失败
    data3 = {"name": rand_str(5), "statusEnum": "B"}
    response = requests.post(url=url, json=data3, headers=headers)
    result = response.json()
    print('响应结果：', response.text)
    assert response.status_code == 400
    assert result["code"] == "saas.bo.e0001_1"
    assert result["msg"] == "状态[ B]已无效，请重新选择其它值"

    print("新增品牌失败，品牌状态码无效")

    # 编辑商品品牌
    url3 = "https://cloud.chanjet.com/hsy/u31tdnagdkg7/mxkx5ur9gc/entity/ProductBrand/%s?" % ProductBrand_id
    data4 = {"code": ProductBrand_code, "name": rand_str(6), "statusEnum": "A"}
    response = requests.put(url=url3, json=data4, headers=headers)
    result = response.json()
    print('响应结果：', response.text)

    assert response.status_code == 200
    print("编辑品牌成功")

    time.sleep(3)

    # 删除商品品牌

    url2 = "https://cloud.chanjet.com/hsy/u31tdnagdkg7/mxkx5ur9gc/entity/ProductBrand/%s?" % ProductBrand_id
    response = requests.delete(url=url2, headers=headers)
    print('响应结果：', response.text)
    assert response.status_code == 200
    print(" 删除品牌成功")

# 调用pytest的main函数执行测试
if __name__ == '__main__':
    pytest.main('ProductBrand.py')